#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: migrate_standard_clamp.py
# @time: 2021/4/19 11:30
# @desc:
from datetime import datetime

from .utils.md5_tools import getFileMd5
from .utils.mongo_tools import total_number_generator, create_one_mongo_value
from .utils.utils import read_json, save_json
from .views.configs import NUMBER_STANDARD_CLAMP_START, MONGO_DATABASE_NAME, STANDARD_CLAMP_COLLECTION
from .views.crud import upload_to_aliyun_standard_clamp_model, send_to_message_queue

standard_clamp = read_json("data/export_data/standard_clamp.json")

for i in standard_clamp:
    print(i["name"])
    i.pop("_id")
    if "version" in i["data"].keys():
        i["version"] = i["data"]["version"]

for i in standard_clamp:
    json_path = f"data/standard_clamp/{i['name']}.json5"
    filename = save_json(i["data"], json_path)
    # temp_dict = {
    #     "number": str(NUMBER_STANDARD_CLAMP_START + total_number_generator(
    #         database_name=MONGO_DATABASE_NAME,
    #         collection_name=STANDARD_CLAMP_COLLECTION,
    #     )),
    #     "time": datetime.now().strftime("%Y-%m-%d %H:%M"),
    #     "name": i["name"],
    #     "version": "2.0",
    #     "isFeature": False,
    #     "isFlag": False,
    #     "type": "3",
    #     "file": {
    #         JSON_OSS_ADDRESS: f"DOCenter_Dev/Pythonfiles/new_data/{json_path}",
    #         "jsonMd5": getFileMd5(filename),
    #         "standardPart": i["data"],
    #         "transferFormatMessage": "not transfered yet",
    #     },
    # }
    standardClampModel = upload_to_aliyun_standard_clamp_model(
        filename=json_path,
        jsonData=i['data'],
        desc="历史数据",
        version="2.0",
        isFeature=False,
        isFlag=False,
        oss_file_dirname=f"DOCenter_Dev/Pythonfiles/new_data",
    )
    create_one_mongo_value(
        standardClampModel,
        database_name=MONGO_DATABASE_NAME,
        collection_name=STANDARD_CLAMP_COLLECTION,
    )
    standardClampModel.pop("_id")
    send_to_message_queue(standardClampModel)
